//
//  SRMainView.m
//  SportRing
//
//  Created by mykoma on 14-4-3.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRMainView.h"
#import "SRSetTargetView.h"
#import "UIScrollView+SVPullToRefresh.h"


@interface SRMainView()<SRSetTargetViewDelegate>
{

}

@property(nonatomic, weak)IBOutlet SRSportView *sportView;
@property(nonatomic, weak)IBOutlet SRSleepingView *sleepingView;
@property(nonatomic, weak)IBOutlet UIButton *backToday;
@property(nonatomic, weak)IBOutlet UISegmentedControl *swichViewSegmentControl;
@property(nonatomic, weak)IBOutlet UIScrollView *containnerScrollView;

@end

@implementation SRMainView

- (void)awakeFromNib
{
    
    [super awakeFromNib];
    
    [self.containnerScrollView setContentSize:CGSizeMake(self.containnerScrollView.frame.size.width, self.containnerScrollView.frame.size.height + 60)];
    
    [self.containnerScrollView addPullToRefreshWithActionHandler:^{
        
        if (_mainDelegate && [_mainDelegate respondsToSelector:@selector(updateData)])
        {
            [_mainDelegate updateData];
        }
    }];
        
}

- (void)refreshData
{
    [_containnerScrollView triggerPullToRefresh];
}

- (void)stopAnimating
{
    [_containnerScrollView.pullToRefreshView stopAnimating];
}

- (void)hideBackTodayButton
{
    CGRect buttonFrame = self.backToday.frame;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.backToday.frame = CGRectMake(self.frame.size.width, buttonFrame.origin.y, buttonFrame.size.width, buttonFrame.size.height);
        
    } completion:^(BOOL finished) {
        
        self.backToday.hidden = YES;
    }];
}

- (void)showBackTodayButton
{
    CGRect buttonFrame = self.backToday.frame;
    
    self.backToday.hidden = NO;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.backToday.frame = CGRectMake(self.frame.size.width-buttonFrame.size.width, buttonFrame.origin.y, buttonFrame.size.width, buttonFrame.size.height);
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)setMainViewDataWithSRSportViewModel:(SRSportViewModel *)srSportViewModel withSRSleepingViewModel:(SRSleepingViewModel*)srSleepingViewModel type:(MAIN_VIEW_TYPE)type
{
    if (type == MAIN_VIEW_TYPE_SLEEPING)
    {
        
        [self.sleepingView setSRSleepingDataViewModel:srSleepingViewModel];
        
    }
    
    if (type == MAIN_VIEW_TYPE_SPORT)
    {
        
        [self.sportView setSRSportViewDataWithModel:srSportViewModel];
        
    }
    
}

-(IBAction)backTodayAction:(id)sender
{
    
    if (_mainDelegate && [_mainDelegate respondsToSelector:@selector(backToday:)])
    {
        
        [_mainDelegate backToday:self];
    }
    
}

-(IBAction)setTarget:(id)sender
{
    SRSetTargetView *srSetTargetView = [SRSetTargetView LoadSRSetTargetView];
    srSetTargetView.srTargetDelegate = self;
    [srSetTargetView show];
}

-(IBAction)swichViewSegmentAction:(id)sender
{
    
    UISegmentedControl *seg = sender;
    
    if (seg.selectedSegmentIndex == 0)
    {
        _type = MAIN_VIEW_TYPE_SPORT;
                
        self.sportView.hidden = NO;
        self.sleepingView.hidden = YES;
    }
    
    if (seg.selectedSegmentIndex == 1)
    {
        _type = MAIN_VIEW_TYPE_SLEEPING;
        
        self.sportView.hidden = YES;
        self.sleepingView.hidden = NO;
    }
    
    if ([_mainDelegate respondsToSelector:@selector(switchType:)])
    {
        [_mainDelegate switchType:_type];
    }
    
}

#pragma mark - SRSportViewDelegate

- (void)onedaySportDetail:(SRSportView *)srSportView
{
    
    NSLog(@"onedaySportDetail");
    
    if (_mainDelegate && [_mainDelegate respondsToSelector:@selector(srMainSportDetail:)])
    {
        [_mainDelegate srMainSportDetail:self];
    }
    
}

#pragma mark - SRSetTargetViewDelegate

- (void)setTargetSteps:(SRSetTargetView *)srSetTargetView{
    
    if (_mainDelegate && [_mainDelegate respondsToSelector:@selector(settingTarget:targetView:)])
    {
        
        [_mainDelegate settingTarget:self targetView:srSetTargetView];
    }

    
}


@end
